package com.ykt.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jfinal.aop.Enhancer;
import com.ykt.cache.CaptchaCache;
import com.ykt.common.sms.qx.ISendWs;
import com.ykt.common.sms.qx.SenderService;
import com.ykt.controller.CaptchaController;

/**
 * 短信服务
 *
 * @author long.hua
 * @version 1.0.0
 * @since 2016-01-10 21:58
 */
public class CaptchaService {
	private final  static Logger LOGGER = LoggerFactory.getLogger(CaptchaService.class);

    private CaptchaCache captchaCache = Enhancer.enhance(CaptchaCache.class);


    public void sendSms(String phone) {
        // 生成验证码
//        String captcha = "123456";
        String captcha = captcha();
        LOGGER.info("手机号{"+phone+"}发送的验证码为："+captcha);
        // 调用短信接口发送短信
        // 千讯短信通道*********************************
        StringBuffer content = new StringBuffer("");
        content.append("验证码：").append(captcha).append("，欢迎您使用金葡萄，请勿将验证码泄露给他人");
        ISendWs ws = (new SenderService()).getSenderPort();
		String resultCode = ws.send("v03599gputao", "8930902c00b1ba078565c9cc", phone, content.toString(), "金葡萄", "");
		LOGGER.info("短信通道方返回结果resultCode===："+resultCode);
		//*********************************************
		
        // 保存到缓存，1分钟失效
        captchaCache.save(phone, captcha);
    }

    // 产生4位随机数
    private String captcha() {
        return String.valueOf((int)((Math.random() * 9+1)*100000));
    }

    public String getCaptcha(String phone){
        return captchaCache.get(phone);
    }

}
